﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AtlasWebApp
{
    public partial class Atlas_EntityDataSource : System.Web.UI.Page
    {
        bool continentsChanged = false;
        bool countriesChanged = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void ButtonEdit_OnClick(object sender, EventArgs args)
        {
            Button buttonEdit = (Button)sender;
            ListViewDataItem item = (ListViewDataItem)buttonEdit.Parent;

            this.ListViewContinents.EditIndex = item.DataItemIndex;
        }

        protected void ButtonSave_OnClick(object sender, EventArgs args)
        {
            continentsChanged = true;
        }

        protected void ButtonInsert_OnClick(object sender, EventArgs args)
        {
            continentsChanged = true;            
        }

        protected void ButtonDelete_OnClick(object sender, EventArgs args)
        {
            continentsChanged = true;
        }

        protected void Page_PreRender(object sender, EventArgs args)
        {
            if (continentsChanged)
            {                
                this.ListBoxContinents.DataBind();                
            }

            if(countriesChanged)
            {                
                this.GridViewCountries.DataBind();               
            }
        }

        public string GetLanguageNameByCountryId(object countryIdObject)
        {            
            int countryId = Int32.Parse(countryIdObject.ToString());
            string languageName = GetLanguageNameByCountryId(countryId);
            return languageName;
        }

        public string GetLanguageNameByCountryId(int countryId)
        {
            using (AtlasEntities context = new AtlasEntities())
            {
                Country country = context.Countries.First(c => c.CountryId == countryId);
                return country.Language.LanguageName;
            }
        }

        public string GetContinentNameByCountryId(object countryIdObject)
        {
            int countryId = Int32.Parse(countryIdObject.ToString());
            string continentName = GetContinentNameByCountryId(countryId);
            return continentName;
        }

        public string GetContinentNameByCountryId(int countryId)
        {
            using (AtlasEntities context = new AtlasEntities())
            {
                Country country = context.Countries.First(c => c.CountryId == countryId);
                return country.Continent.ContinentName;
            }
        }

        protected void ButtonInsertNewCountry_OnClick(object sender, EventArgs args)
        {
            Button buttonInsert = (Button)sender;

            DataControlFieldCell cell = (DataControlFieldCell)(buttonInsert.Parent);
            GridViewRow row = (GridViewRow)(cell.Parent);

            string countryName = null;
            int languageId = -1;
            int continentId = -1;
            int flagId = -1;

            foreach (DataControlFieldCell childCell in row.Controls)
            {
                foreach (Control childControl in childCell.Controls)
                {
                    TextBox textBoxCountryName = childControl as TextBox;

                    if (textBoxCountryName != null)
                    {
                        countryName = textBoxCountryName.Text;
                    }

                    DropDownList dropDownListLanguages = childControl as DropDownList;

                    if (dropDownListLanguages != null)
                    {
                        if (dropDownListLanguages.ID == "DropDownListInsertLanguages")
                        {
                            string languageIdString = dropDownListLanguages.SelectedValue.ToString();
                            languageId = Int32.Parse(languageIdString);
                        }

                        if (dropDownListLanguages.ID == "DropDownListInsertContinentId")
                        {
                            string continentIdString = dropDownListLanguages.SelectedValue.ToString();
                            continentId = Int32.Parse(continentIdString);
                        }
                    }

                    if (childControl.ID == "UpdatePanelFlags_Insert")
                    {
                        foreach (Control updatePanelChild in childControl.Controls[0].Controls)
                        {
                            if (updatePanelChild.ID == "DropDownListFlags_Insert")
                            {
                                DropDownList dropDownListFlags = (DropDownList)updatePanelChild;
                                flagId = Int32.Parse(dropDownListFlags.SelectedValue);
                            }
                        }
                    }
                }
            }

            if (countryName != null && languageId != -1 && continentId != -1 && flagId != -1)
            {
                using (AtlasEntities context = new AtlasEntities())
                {
                    Country country = new Country() { CountryName = countryName, ContinentId = continentId, LanguageId = languageId, FlagId = flagId };
                    context.Countries.AddObject(country);
                    context.SaveChanges();
                    countriesChanged = true;
                }
            }
        }

        //protected override void OnError(EventArgs e)
        //{
        //    //this.LiteralErrorMessage.Text = "error man";
        //    //Exception exception = HttpContext.Current.Server.GetLastError();
        //    //HttpContext.Current.Server.ClearError();            

        //    Response.Redirect("~/ErrorPage.aspx");            
        //}
    }
}